//index.js
//获取应用实例
var app = getApp()
var CommentUtils = app.CommonUtils;
var Keys = require('../../assets/data/keys.js')
Page({
	data: {
		questionTypeList: [],
		cityList: [],
		cityListIndex: 0,  //城市列表选择第几项
		AD_image: '',
    datas:'',


    userInfo:'',
    nickName:'',
    avatarUrl:'',
    gender:'',
    province:'',
    city:'',
    country:'',



	},

	onLoad: function () {
		var _this = this;


    try {
      var value = wx.getStorageSync('city')
      if (value) {
        // Do something with return value
        if(value.length == 0);
        console.log("可以");
      }else{
        console.log("不可以");
        try {
          wx.setStorageSync('city',"1")
        } catch (e) {
        }
      }
    } catch (e) {
      // Do something when catch error
    }

    try {
      wx.setStorageSync('city', "1")
      var value = wx.getStorageSync('city')
      if (value) {
        // Do something with return value
        console.log("完成");
        console.log(value);
      }
    } catch (e) {
      // Do something when catch error
    }

    
    // try {
    //   wx.setStorageSync('Token', 'jvFRBW4rQP')
    // } catch (e) {
    // }
		wx.login({
    
			success: function (res) {
        console.log("js_code");
        console.log(res);
        console.log("js_code");
				wx.request({
          url: "https://www.chnode.xyz/api/Bubble.ashx?",
					data: {
            DataType: "Get_JSPIOpenID",
            js_code: res.code.toString(),
					},
					success: function (res) {
            console.log(res);
						if (res.data.isok == "true") {
							console.log(res.data);
							app.open_id = res.data.data.openid
              console.log("3333333");
              console.log(app.open_id);
              console.log("4444444");

            console.log("sssss");
              console.log(app.open_id);
						}
					}
				})
			}
		})
		wx.request({
      url: "https://www.chnode.xyz/api/Home.ashx?DataType=City_List",
			success: function (res) {
        console.log(res);
				_this.setData({
					cityList: res.data.datas,
				});
			}
		})
		wx.request({
      url: "https://www.chnode.xyz/api/Home.ashx?DataType=Question_Type",
			success: function (res) {
        
				_this.setData({
					questionTypeList: res.data.datas,
				});
        console.log(_this.data.questionTypeList);
			}
		})
		wx.request({
      url: "https://www.chnode.xyz/api/Home.ashx?DataType=AD",
			success: function (res) {
				_this.setData({
					AD_image: res.data.data.A_Image,
				});
			}
		})

    wx.request({
      url: "https://www.chnode.xyz/api/Home.ashx?",
      data: {
        DataType: "Question_List",
      },
      success: function (res) {
        console.log("aaaa");
        console.log(res)
        if (res.data.isok == "false")
          _this.showZanTopTips(res.data.info);
        else {
          _this.setData({
            datas: res.data.datas
          })
          console.log(_this.data.datas)
        }
      }
    })

   

    wx.login({
     
      success: function (res) {
        console.log("1111111");
        console.log(res);
        console.log("333333");


        //////////////

        wx.getUserInfo({
          success: function (res) {
            var userInfo = res.userInfo
            var nickName = userInfo.nickName
            var avatarUrl = userInfo.avatarUrl
            var gender = userInfo.gender //性别 0：未知、1：男、2：女
            var province = userInfo.province
            var city = userInfo.city
            var country = userInfo.country
            console.log("sssssss");
            console.log(userInfo.avatarUrl);
            console.log(gender);
            console.log(province);
            console.log(city);
            console.log("sssssss");
            _this.setData({
              nickName: nickName,
              avatarUrl: avatarUrl,
              gender: gender,
              province: province,
              city: city,
              country: country
            })
            console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
            console.log(_this.data.gender);
            
          }
        })

        /////////////

        wx.request({
          url: "https://www.chnode.xyz/api/Bubble.ashx?",
          data: {
            DataType: "Get_JSPIOpenID",
            js_code: res.code.toString(),
          },
          success: function (res) {
      console.log(res);
            if (res.data.isok == "true") {
              /////////////
              if (_this.data.gender == 1){
                _this.setData({
                  gender: "男",
                });
              } else if (_this.data.gender == 2){
                _this.setData({
                  gender: "女",
                });
              }

              console.log("bbbbbbbbbbbbbb");
              console.log(_this.data.gender);

              wx.request({
                url: "https://www.chnode.xyz/api/User.ashx?",
                data: {
                  DataType: "UserInfoSave",
                  openid: res.data.data.openid,
                  Country: _this.data.country,
                  Privince: _this.data.province,
                  City: _this.data.city,
                  HeadimgUrl: _this.data.avatarUrl,
                  NickName: _this.data.nickName,
                  Sex: _this.data.gender
                },

                success: function (res) {
                  console.log("aaaaaaaaaaaaaaaaaaaaaaaaa123123");
                  console.log(res)
                  if (res.data.isok == "false") {

                  }

                  else {


                  }
                }
              })

              /////////////
              console.log("sssss");
              console.log(res.data.data.openid);
              try {
                wx.setStorageSync('openid', res.data.data.openid)
              } catch (e) {
              }
              console.log("ssssdddd");
            }
          }
        })
      }
    })


	},

	/**
	 * 城市列表选择
	 */
	bindCityPickerChange: function (e) {
    var _this = this;
		var index = e.detail.value;
    console.log(_this.data.cityList[index].C_ID);
    try {
      wx.setStorageSync('city', _this.data.cityList[index].C_ID)
    } catch (e) {
    }
		this.setData({
			cityListIndex: index,
		})
	},

	/**
	 * 问题列表点击事件
	 */
	btnEventQuestionTypeItem: function (e) {
		var _this = this;
		var index = e.currentTarget.dataset.index;
    console.log(_this.data.questionTypeList[index].QT_ID);
		console.log(index)
    
    try {
      var value = wx.getStorageSync('Token')
      if (value) {
        // Do something with return value
        wx.navigateTo({
          url: '../question_list/index?id=' + _this.data.questionTypeList[index].QT_ID,
        })
      }else{
        wx.navigateTo({
          url: '../f_binding_phone/index?',
        })
      }
    } catch (e) {
      // Do something when catch error
    }



	},

  push: function (e) {
    var _this = this;
    console.log(e.currentTarget.dataset.index);
    try {
      var value = wx.getStorageSync('Token')
      if (value) {
        // Do something with return value
        console.log(_this.data.datas[e.currentTarget.dataset.index].Q_ID);
        wx.navigateTo({
          url: "../question-detail/index?id=" + _this.data.datas[e.currentTarget.dataset.index].Q_ID
        })

      } else {
        wx.navigateTo({
          url: '../f_binding_phone/index?',
        })
      }
    } catch (e) {
      // Do something when catch error
    }


  },

  /**
   * 搜索
   */
	btnEventSearch: function (e) {
    console.log("sousuo");
    var _this = this;
    var temp_input = e.detail.value;
    try {
      var value = wx.getStorageSync('Token')
      if (value) {
        // Do something with return value
        console.log(value);
        console.log("shuchu");

        wx.navigateTo({
          url: '../search-list/index?id=' + temp_input,
        })
        console.log(temp_input);
      }else{
        console.log("bucunzai");
        wx.navigateTo({
          url: '../f_binding_phone/index?',
        })
      }
    } catch (e) {
      console.log(e);
      console.log("meishuchu");
      
      // Do something when catch error
    }


		// wx.request({
		// 	url: Keys.data.url_Product,
		// 	data: {
		// 		DataType: "Product_Query",
		// 		name: temp_input,
		// 	},
		// 	success: function (res) {
		// 		if (res.data.isok == "true") {
		// 			if (res.data.datas.length > 0) {
		// 				wx.setStorage({
		// 					key: 'search',
		// 					data: res.data.datas,
		// 					success: function () {
								
		// 					}, fail: function () { CommentUtils.showToastWithImg("稍后尝试", 2) }
		// 				})
		// 			} else {
		// 				CommentUtils.showToastWithImg("未找到商品", 3)
		// 			}
		// 		} else {
		// 			CommentUtils.showToastWithImg("稍后尝试", 2)
		// 		}
		// 	}
		// })
	}
})

